﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Text;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYTextTool.Properties;

namespace HYTextTool
{
	public partial class TextToolForm : System.Windows.Forms.Form
	{
		public TextToolForm()
		{
			this.InitializeComponent();
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.ReshowDelay = 100;
			toolTip.SetToolTip(this.btnSubscript, "下标");
			toolTip.SetToolTip(this.btnSuperscript, "上标");
			toolTip.SetToolTip(this.btnColor, "颜色");
			toolTip.SetToolTip(this.btnBold, "加粗");
			toolTip.SetToolTip(this.btnItalic, "斜体");
			toolTip.SetToolTip(this.btnUnderline, "下划线");
			toolTip.SetToolTip(this.btnDegree, "角度");
			toolTip.SetToolTip(this.btnAddSubtract, "正负号");
			toolTip.SetToolTip(this.btnDiameter, "直径");
			toolTip.SetToolTip(this.btnPercent, "百分号");
			toolTip.SetToolTip(this.btnLevel1, "一级钢");
			toolTip.SetToolTip(this.btnLevel2, "二级钢");
			toolTip.SetToolTip(this.btnLevel3, "三级钢");
			toolTip.SetToolTip(this.btnLevel4, "四级钢");
			toolTip.SetToolTip(this.btnSpecialCharacter, "特殊字符");
			toolTip.SetToolTip(this.btnPickInScreen, "屏幕取词");
			List<string> windowsFontNames = Common.GetWindowsFontNames();
			this.cmbFont.DataSource = windowsFontNames;
			List<string> dataSource = new List<string>(new string[]
			{
				"左对齐",
				"右对齐",
				"中心对齐"
			});
			this.cmbAlign.DataSource = dataSource;
			List<string> dataSource2 = new List<string>(new string[]
			{
				"横向",
				"竖向"
			});
			this.cmbFormat.DataSource = dataSource2;
			if (!this.ReadData())
			{
				this.cmbFont.Text = "宋体";
				this.tbSize.Text = "4";
				this.tbRowDistance.Text = "0.4";
				this.tbPageWidth.Text = "200";
				this.tbPageHeight.Text = "200";
				this.tbPageDistance.Text = "7";
				this.tbAngle.Text = "0";
			}
			this.richTextBox1.Select();
			this.richTextBox1.SelectionFont = new Font(this.cmbFont.Text, float.Parse(this.tbSize.Text), GraphicsUnit.Millimeter);
			this.TextToolForm_Resize(this, new EventArgs());
		}

		public void EditText(List<Text> texts)
		{
			if (texts != null)
			{
				this.richTextBox1.Focus();
				foreach (Text text in texts)
				{
					FontStyle fontStyle = FontStyle.Regular;
					if (text.Bold)
					{
						fontStyle |= FontStyle.Bold;
					}
					if (text.Italic)
					{
						fontStyle |= FontStyle.Italic;
					}
					if (text.Underline)
					{
						fontStyle |= FontStyle.Underline;
					}
					this.richTextBox1.SelectionFont = new Font(text.Font, (float)text.Height, fontStyle, GraphicsUnit.Millimeter);
					this.richTextBox1.SelectionAlignment = (HorizontalAlignment)text.Alignment;
					if (text.Superscript)
					{
						this.richTextBox1.SelectionCharOffset = (int)(text.Height / 0.3528);
					}
					else if (text.Subscript)
					{
						this.richTextBox1.SelectionCharOffset = -1;
					}
					else
					{
						this.richTextBox1.SelectionCharOffset = 0;
					}
					this.richTextBox1.SelectionColor = text.Color;
					if (Encoding.GetEncoding("iso-8859-1").GetString(new byte[]
					{
						197
					}) == text.Name)
					{
						this.richTextBox1.SelectedText = "\n";
					}
					else
					{
						this.richTextBox1.SelectedText = text.Name;
					}
				}
			}
		}

		public void InsertText(List<Text> texts)
		{
			if (texts.Count > 0)
			{
				if (this.richTextBox1.TextLength > 0)
				{
					this.richTextBox1.Focus();
					this.richTextBox1.Select(this.richTextBox1.TextLength - 1, 1);
					Font selectionFont = this.richTextBox1.SelectionFont;
					this.richTextBox1.Select(this.richTextBox1.TextLength, 0);
					using (List<Text>.Enumerator enumerator = texts.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Text text = enumerator.Current;
							Encoding encoding = Encoding.GetEncoding("iso-8859-1");
							if (encoding.GetString(new byte[]
							{
								197
							}) == text.Name)
							{
								this.richTextBox1.SelectedText = "\n";
							}
							else if (WindowsCharacterSet.UnicodeToString("AC00") == text.Name || WindowsCharacterSet.UnicodeToString("AC01") == text.Name || WindowsCharacterSet.UnicodeToString("AC02") == text.Name || WindowsCharacterSet.UnicodeToString("AC03") == text.Name || encoding.GetString(new byte[]
							{
								196
							}) == text.Name)
							{
								this.richTextBox1.SelectionFont = new Font("HY Arial Unicode MS v1", selectionFont.Size);
								this.richTextBox1.SelectedText = text.Name;
							}
							else
							{
								if (text.Superscript)
								{
									this.richTextBox1.SelectionFont = new Font(selectionFont.FontFamily, selectionFont.Size / 2f);
									this.richTextBox1.SelectionCharOffset = (int)(selectionFont.Size / 2f);
								}
								else if (text.Subscript)
								{
									this.richTextBox1.SelectionFont = new Font(selectionFont.FontFamily, selectionFont.Size / 2f);
									this.richTextBox1.SelectionCharOffset = -1;
								}
								else
								{
									this.richTextBox1.SelectionFont = new Font(selectionFont.FontFamily, selectionFont.Size);
									this.richTextBox1.SelectionCharOffset = 0;
								}
								this.richTextBox1.SelectedText = text.Name;
							}
						}
						return;
					}
				}
				foreach (Text text2 in texts)
				{
					FontStyle fontStyle = FontStyle.Regular;
					if (text2.Bold)
					{
						fontStyle |= FontStyle.Bold;
					}
					if (text2.Italic)
					{
						fontStyle |= FontStyle.Italic;
					}
					if (text2.Underline)
					{
						fontStyle |= FontStyle.Underline;
					}
					this.richTextBox1.SelectionFont = new Font(text2.Font, (float)text2.Height, fontStyle, GraphicsUnit.Millimeter);
					this.richTextBox1.SelectionAlignment = (HorizontalAlignment)text2.Alignment;
					if (text2.Superscript)
					{
						this.richTextBox1.SelectionCharOffset = (int)(text2.Height / 0.3528);
					}
					else if (text2.Subscript)
					{
						this.richTextBox1.SelectionCharOffset = -1;
					}
					else
					{
						this.richTextBox1.SelectionCharOffset = 0;
					}
					this.richTextBox1.SelectionColor = text2.Color;
					if (Encoding.GetEncoding("iso-8859-1").GetString(new byte[]
					{
						197
					}) == text2.Name)
					{
						this.richTextBox1.SelectedText = "\n";
					}
					else
					{
						this.richTextBox1.SelectedText = text2.Name;
					}
				}
			}
		}

		private void btnSpecialCharacter_Click(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			SpecialCharacterForm specialCharacterForm = new SpecialCharacterForm(this.m_specialType);
			if (DialogResult.OK == specialCharacterForm.ShowDialog())
			{
				this.m_specialType = specialCharacterForm.m_selType;
				if (string.Empty != specialCharacterForm.m_selCharater)
				{
					this.richTextBox1.Focus();
					this.richTextBox1.SelectedText = specialCharacterForm.m_selCharater;
					return;
				}
			}
			this.richTextBox1.Focus();
			this.richTextBox1.Select(selectionStart, 0);
		}

		private void btnColor_Click(object sender, EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();
			if (DialogResult.OK == colorDialog.ShowDialog())
			{
				this.richTextBox1.SelectionColor = colorDialog.Color;
				this.m_color = colorDialog.Color;
				(sender as Button).Invalidate();
			}
		}

		private void btnColor_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
			Pen pen = new Pen(this.m_color, 2f);
			graphics.DrawLine(pen, 8, 22, 21, 22);
			Font font = new Font("微软雅黑", 12f);
			graphics.DrawString("A", font, Brushes.Black, new PointF(6f, 3f));
		}

		private void cmbFont_SelectedIndexChanged(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			if (selectionLength == 0)
			{
				Font selectionFont = this.richTextBox1.SelectionFont;
				Font selectionFont2 = new Font(this.cmbFont.Text, selectionFont.Size, selectionFont.Style);
				this.richTextBox1.SelectionFont = selectionFont2;
				this.richTextBox1.Focus();
				this.richTextBox1.SelectionStart = selectionStart;
				return;
			}
			string a = WindowsCharacterSet.UnicodeToString("AC00");
			string a2 = WindowsCharacterSet.UnicodeToString("AC01");
			string a3 = WindowsCharacterSet.UnicodeToString("AC02");
			string a4 = WindowsCharacterSet.UnicodeToString("AC03");
			for (int i = 0; i < selectionLength; i++)
			{
				this.richTextBox1.SelectionStart = selectionStart + i;
				this.richTextBox1.SelectionLength = 1;
				if (!(a == this.richTextBox1.SelectedText) && !(a2 == this.richTextBox1.SelectedText) && !(a3 == this.richTextBox1.SelectedText) && !(a4 == this.richTextBox1.SelectedText))
				{
					Font selectionFont3 = this.richTextBox1.SelectionFont;
					Font selectionFont4 = new Font(this.cmbFont.Text, selectionFont3.Size, selectionFont3.Style);
					this.richTextBox1.SelectionFont = selectionFont4;
				}
			}
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void cmbAlign_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.richTextBox1.SelectionAlignment = (HorizontalAlignment)this.cmbAlign.SelectedIndex;
		}

		private void btnSubscript_Click(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			for (int i = 0; i < selectionLength; i++)
			{
				this.richTextBox1.SelectionStart = selectionStart + i;
				this.richTextBox1.SelectionLength = 1;
				if (this.richTextBox1.SelectionCharOffset == 0)
				{
					Font selectionFont = this.richTextBox1.SelectionFont;
					Font selectionFont2 = new Font(selectionFont.Name, selectionFont.Size / 2f, selectionFont.Style);
					this.richTextBox1.SelectionFont = selectionFont2;
					this.richTextBox1.SelectionCharOffset = -1;
				}
				else if (-1 == this.richTextBox1.SelectionCharOffset)
				{
					Font selectionFont3 = this.richTextBox1.SelectionFont;
					Font selectionFont4 = new Font(selectionFont3.Name, selectionFont3.Size * 2f, selectionFont3.Style);
					this.richTextBox1.SelectionFont = selectionFont4;
					this.richTextBox1.SelectionCharOffset = 0;
				}
				else
				{
					this.richTextBox1.SelectionCharOffset = -1;
				}
			}
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void btnSuperscript_Click(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			for (int i = 0; i < selectionLength; i++)
			{
				this.richTextBox1.SelectionStart = selectionStart + i;
				this.richTextBox1.SelectionLength = 1;
				Font selectionFont = this.richTextBox1.SelectionFont;
				if (this.richTextBox1.SelectionCharOffset == 0)
				{
					Font selectionFont2 = new Font(selectionFont.Name, selectionFont.Size / 2f, selectionFont.Style);
					this.richTextBox1.SelectionFont = selectionFont2;
					this.richTextBox1.SelectionCharOffset = (int)selectionFont.Size / 2;
				}
				else if (this.richTextBox1.SelectionCharOffset > 0 && (float)this.richTextBox1.SelectionCharOffset < selectionFont.Size)
				{
					Font selectionFont3 = new Font(selectionFont.Name, selectionFont.Size * 2f, selectionFont.Style);
					this.richTextBox1.SelectionFont = selectionFont3;
					this.richTextBox1.SelectionCharOffset = 0;
				}
				else
				{
					this.richTextBox1.SelectionCharOffset = (int)selectionFont.Size;
				}
			}
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void tbSize_Leave(object sender, EventArgs e)
		{
			System.Drawing.Point mousePosition = Control.MousePosition;
			System.Drawing.Point pt = base.PointToClient(mousePosition);
			if (this.btnCancel.Bounds.Contains(pt))
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbSize.Text);
				if (num <= 0.0 || num > 100.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				MessageBox.Show("文字大小范围在0~100mm之间。", "盈建科提示");
				this.tbSize.Focus();
				this.tbSize.SelectAll();
				return;
			}
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			for (int i = 0; i < selectionLength; i++)
			{
				this.richTextBox1.SelectionStart = selectionStart + i;
				this.richTextBox1.SelectionLength = 1;
				Font selectionFont = this.richTextBox1.SelectionFont;
				if (this.richTextBox1.SelectionCharOffset == 0)
				{
					Font selectionFont2 = new Font(selectionFont.Name, float.Parse(this.tbSize.Text), selectionFont.Style, GraphicsUnit.Millimeter);
					this.richTextBox1.SelectionFont = selectionFont2;
				}
				else if (this.richTextBox1.SelectionCharOffset < 0)
				{
					Font selectionFont3 = new Font(selectionFont.Name, float.Parse(this.tbSize.Text) / 2f, selectionFont.Style, GraphicsUnit.Millimeter);
					this.richTextBox1.SelectionFont = selectionFont3;
				}
				else
				{
					Font selectionFont4 = new Font(selectionFont.Name, float.Parse(this.tbSize.Text) / 2f, selectionFont.Style, GraphicsUnit.Millimeter);
					this.richTextBox1.SelectionFont = selectionFont4;
					this.richTextBox1.SelectionCharOffset = (int)float.Parse(this.tbSize.Text) / 2;
				}
			}
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void btnBold_Click(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			for (int i = selectionStart; i < selectionStart + selectionLength; i++)
			{
				this.richTextBox1.Select(i, 1);
				Font selectionFont = this.richTextBox1.SelectionFont;
				Font selectionFont2;
				if (selectionFont.Bold)
				{
					selectionFont2 = new Font(selectionFont, selectionFont.Style & ~FontStyle.Bold);
				}
				else
				{
					selectionFont2 = new Font(selectionFont, selectionFont.Style | FontStyle.Bold);
				}
				this.richTextBox1.SelectionFont = selectionFont2;
			}
			this.richTextBox1.Focus();
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void btnItalic_Click(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			for (int i = selectionStart; i < selectionStart + selectionLength; i++)
			{
				this.richTextBox1.Select(i, 1);
				Font selectionFont = this.richTextBox1.SelectionFont;
				Font selectionFont2;
				if (selectionFont.Italic)
				{
					selectionFont2 = new Font(selectionFont, selectionFont.Style & ~FontStyle.Italic);
				}
				else
				{
					selectionFont2 = new Font(selectionFont, selectionFont.Style | FontStyle.Italic);
				}
				this.richTextBox1.SelectionFont = selectionFont2;
			}
			this.richTextBox1.Focus();
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void btnUnderline_Click(object sender, EventArgs e)
		{
			int selectionStart = this.richTextBox1.SelectionStart;
			int selectionLength = this.richTextBox1.SelectionLength;
			for (int i = selectionStart; i < selectionStart + selectionLength; i++)
			{
				this.richTextBox1.Select(i, 1);
				Font selectionFont = this.richTextBox1.SelectionFont;
				Font selectionFont2;
				if (selectionFont.Underline)
				{
					selectionFont2 = new Font(selectionFont, selectionFont.Style & ~FontStyle.Underline);
				}
				else
				{
					selectionFont2 = new Font(selectionFont, selectionFont.Style | FontStyle.Underline);
				}
				this.richTextBox1.SelectionFont = selectionFont2;
			}
			this.richTextBox1.Focus();
			this.richTextBox1.SelectionStart = selectionStart;
			this.richTextBox1.SelectionLength = selectionLength;
		}

		private void btnDegree_Click(object sender, EventArgs e)
		{
			this.richTextBox1.Focus();
			this.richTextBox1.SelectedText = "°";
		}

		private void btnAddSubtract_Click(object sender, EventArgs e)
		{
			this.richTextBox1.Focus();
			this.richTextBox1.SelectedText = "±";
		}

		private void btnDiameter_Click(object sender, EventArgs e)
		{
			this.richTextBox1.Focus();
			this.richTextBox1.SelectedText = "Ф";
		}

		private void btnPercent_Click(object sender, EventArgs e)
		{
			this.richTextBox1.Focus();
			this.richTextBox1.SelectedText = "%";
		}

		private void btnLevel1_Click(object sender, EventArgs e)
		{
			string selectedText = WindowsCharacterSet.UnicodeToString("AC00");
			this.richTextBox1.Focus();
			int selectionStart = this.richTextBox1.SelectionStart;
			this.richTextBox1.SelectedText = selectedText;
			this.richTextBox1.Select(selectionStart, 1);
			this.richTextBox1.SelectionFont = new Font("HY Arial Unicode MS v1", float.Parse(this.tbSize.Text), GraphicsUnit.Millimeter);
			this.richTextBox1.Select(selectionStart + 1, 0);
		}

		private void btnLevel2_Click(object sender, EventArgs e)
		{
			string selectedText = WindowsCharacterSet.UnicodeToString("AC01");
			this.richTextBox1.Focus();
			int selectionStart = this.richTextBox1.SelectionStart;
			this.richTextBox1.SelectedText = selectedText;
			this.richTextBox1.Select(selectionStart, 1);
			this.richTextBox1.SelectionFont = new Font("HY Arial Unicode MS v1", float.Parse(this.tbSize.Text), GraphicsUnit.Millimeter);
			this.richTextBox1.Select(selectionStart + 1, 0);
		}

		private void btnLevel3_Click(object sender, EventArgs e)
		{
			string selectedText = WindowsCharacterSet.UnicodeToString("AC02");
			this.richTextBox1.Focus();
			int selectionStart = this.richTextBox1.SelectionStart;
			this.richTextBox1.SelectedText = selectedText;
			this.richTextBox1.Select(selectionStart, 1);
			this.richTextBox1.SelectionFont = new Font("HY Arial Unicode MS v1", float.Parse(this.tbSize.Text), GraphicsUnit.Millimeter);
			this.richTextBox1.Select(selectionStart + 1, 0);
		}

		private void btnLevel4_Click(object sender, EventArgs e)
		{
			string selectedText = WindowsCharacterSet.UnicodeToString("AC03");
			this.richTextBox1.Focus();
			int selectionStart = this.richTextBox1.SelectionStart;
			this.richTextBox1.SelectedText = selectedText;
			this.richTextBox1.Select(selectionStart, 1);
			this.richTextBox1.SelectionFont = new Font("HY Arial Unicode MS v1", float.Parse(this.tbSize.Text), GraphicsUnit.Millimeter);
			this.richTextBox1.Select(selectionStart + 1, 0);
		}

		private void btnFindNext_Click(object sender, EventArgs e)
		{
			if (string.Empty == this.tbOriginText.Text)
			{
				return;
			}
			int start = this.richTextBox1.SelectionStart + this.richTextBox1.SelectionLength;
			RichTextBoxFinds options = RichTextBoxFinds.None;
			if (this.ckbCase.Checked)
			{
				options = RichTextBoxFinds.MatchCase;
			}
			int num = this.richTextBox1.Find(this.tbOriginText.Text, start, options);
			if (-1 == num)
			{
				num = this.richTextBox1.Find(this.tbOriginText.Text, 0, options);
			}
			if (-1 == num)
			{
				MessageBox.Show("无匹配项。");
			}
		}

		private void btnReplace_Click(object sender, EventArgs e)
		{
			string a;
			string b;
			if (this.ckbCase.Checked)
			{
				a = this.tbOriginText.Text;
				b = this.richTextBox1.SelectedText;
			}
			else
			{
				a = this.tbOriginText.Text.ToUpper();
				b = this.richTextBox1.SelectedText.ToUpper();
			}
			if (a != b)
			{
				int start = this.richTextBox1.SelectionStart + this.richTextBox1.SelectionLength;
				RichTextBoxFinds options = RichTextBoxFinds.None;
				if (this.ckbCase.Checked)
				{
					options = RichTextBoxFinds.MatchCase;
				}
				int num = this.richTextBox1.Find(this.tbOriginText.Text, start, options);
				if (-1 == num)
				{
					num = this.richTextBox1.Find(this.tbOriginText.Text, 0, options);
				}
				if (-1 == num)
				{
					MessageBox.Show("无匹配项。");
					return;
				}
			}
			else
			{
				int num2 = this.richTextBox1.SelectionStart + this.richTextBox1.SelectionLength;
				this.richTextBox1.SelectedText = this.tbReplaceText.Text;
				RichTextBoxFinds options2 = RichTextBoxFinds.None;
				if (this.ckbCase.Checked)
				{
					options2 = RichTextBoxFinds.MatchCase;
				}
				if (this.tbOriginText.TextLength <= num2)
				{
					return;
				}
				int num3 = this.richTextBox1.Find(this.tbOriginText.Text, num2, options2);
				if (-1 == num3)
				{
					num3 = this.richTextBox1.Find(this.tbOriginText.Text, 0, options2);
				}
				if (-1 == num3)
				{
					MessageBox.Show("无匹配项。");
					return;
				}
			}
		}

		private void btnReplaceAll_Click(object sender, EventArgs e)
		{
			if (this.tbOriginText.Text.Length == 0 || this.tbReplaceText.Text.Length == 0 || this.richTextBox1.Text.Length == 0)
			{
				return;
			}
			for (;;)
			{
				string a;
				string b;
				if (this.ckbCase.Checked)
				{
					a = this.tbOriginText.Text;
					b = this.richTextBox1.SelectedText;
				}
				else
				{
					a = this.tbOriginText.Text.ToUpper();
					b = this.richTextBox1.SelectedText.ToUpper();
				}
				if (a != b)
				{
					int start = this.richTextBox1.SelectionStart + this.richTextBox1.SelectionLength;
					RichTextBoxFinds options = RichTextBoxFinds.None;
					if (this.ckbCase.Checked)
					{
						options = RichTextBoxFinds.MatchCase;
					}
					int num = this.richTextBox1.Find(this.tbOriginText.Text, start, options);
					if (-1 == num)
					{
						num = this.richTextBox1.Find(this.tbOriginText.Text, 0, options);
					}
					if (-1 == num)
					{
						break;
					}
				}
				else
				{
					int selectionStart = this.richTextBox1.SelectionStart;
					int selectionLength = this.richTextBox1.SelectionLength;
					this.richTextBox1.SelectedText = this.tbReplaceText.Text;
				}
			}
			MessageBox.Show("替换完成。");
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.richTextBox1.TextLength < 1)
			{
				base.DialogResult = DialogResult.None;
				MessageBox.Show("请在文本框输入文本。", "盈建科提示");
			}
			Article.g_PageWidth = double.Parse(this.tbPageWidth.Text);
			Article.g_PageHeight = double.Parse(this.tbPageHeight.Text);
			Article.g_RowInterval = double.Parse(this.tbRowDistance.Text);
			Article.g_PageInterval = double.Parse(this.tbPageDistance.Text);
			Article.g_Format = this.cmbFormat.SelectedIndex;
			Article.g_Angle = double.Parse(this.tbAngle.Text);
			this.m_pickInScreen = false;
			this.m_sections = this.GetSectionText();
		}

		public List<Section> GetSectionText()
		{
			List<Section> list = new List<Section>();
			List<Text> list2 = new List<Text>();
			this.m_scripts.Clear();
			this.m_aligns.Clear();
			this.m_length = this.richTextBox1.TextLength;
			for (int i = 0; i < this.m_length; i++)
			{
				this.richTextBox1.Select(i, 1);
				Text text = new Text();
				text.Name = this.richTextBox1.SelectedText;
				text.Font = this.richTextBox1.SelectionFont.Name;
				text.Height = (double)this.richTextBox1.SelectionFont.Size * 0.3528;
				text.Height = Math.Round(text.Height, 1);
				text.Width = text.Height;
				if (Common.GetLength(text.Name) == 1)
				{
					text.Width /= 2.0;
				}
				text.Width = Math.Round(text.Width, 1);
				text.Color = this.richTextBox1.SelectionColor;
				if (this.richTextBox1.SelectionCharOffset > 0)
				{
					text.Superscript = true;
					text.Subscript = false;
					this.m_scripts.Add(i, 1);
				}
				else if (this.richTextBox1.SelectionCharOffset < 0)
				{
					text.Superscript = false;
					text.Subscript = true;
					this.m_scripts.Add(i, 2);
				}
				else if (this.richTextBox1.SelectionCharOffset == 0)
				{
					text.Superscript = false;
					text.Subscript = false;
				}
				text.Bold = this.richTextBox1.SelectionFont.Bold;
				text.Italic = this.richTextBox1.SelectionFont.Italic;
				text.Underline = this.richTextBox1.SelectionFont.Underline;
				text.Alignment = (int)this.richTextBox1.SelectionAlignment;
				list2.Add(text);
				if ("\n" == this.richTextBox1.SelectedText)
				{
					Section item = new Section(list2);
					list.Add(item);
					list2 = new List<Text>();
					this.m_aligns.Add(i, text.Alignment);
				}
			}
			this.richTextBox1.Select(this.m_length, 0);
			if (list2.Count > 0)
			{
				Section item2 = new Section(list2);
				list.Add(item2);
				this.m_aligns.Add(this.m_length - 1, list2[list2.Count - 1].Alignment);
			}
			return list;
		}

		private void btnPickInScreen_Click(object sender, EventArgs e)
		{
			this.m_pickInScreen = true;
		}

		private void tbRowDistance_Leave(object sender, EventArgs e)
		{
			System.Drawing.Point mousePosition = Control.MousePosition;
			System.Drawing.Point pt = base.PointToClient(mousePosition);
			if (this.btnCancel.Bounds.Contains(pt))
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbRowDistance.Text);
				if (num <= 0.0 || num > 10.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				MessageBox.Show("行间距范围在0~10mm之间。");
				this.tbRowDistance.Focus();
				this.tbRowDistance.SelectAll();
			}
		}

		private void tbPageWidth_Leave(object sender, EventArgs e)
		{
			System.Drawing.Point mousePosition = Control.MousePosition;
			System.Drawing.Point pt = base.PointToClient(mousePosition);
			if (this.btnCancel.Bounds.Contains(pt))
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbPageWidth.Text);
				if (num < 100.0 || num > 1000.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				MessageBox.Show("页宽范围在100~1000mm之间。");
				this.tbPageWidth.Focus();
				this.tbPageWidth.SelectAll();
			}
		}

		private void tbPageHeight_Leave(object sender, EventArgs e)
		{
			System.Drawing.Point mousePosition = Control.MousePosition;
			System.Drawing.Point pt = base.PointToClient(mousePosition);
			if (this.btnCancel.Bounds.Contains(pt))
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbPageHeight.Text);
				if (num < 100.0 || num > 1000.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				MessageBox.Show("页高范围在100~1000mm之间。");
				this.tbPageHeight.Focus();
				this.tbPageHeight.SelectAll();
			}
		}

		private void tbPageDistance_Leave(object sender, EventArgs e)
		{
			System.Drawing.Point mousePosition = Control.MousePosition;
			System.Drawing.Point pt = base.PointToClient(mousePosition);
			if (this.btnCancel.Bounds.Contains(pt))
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbPageDistance.Text);
				if (num <= 0.0 || num > 100.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				MessageBox.Show("页间距范围在0~100mm之间。");
				this.tbPageDistance.Focus();
				this.tbPageDistance.SelectAll();
			}
		}

		private void tbAngle_Leave(object sender, EventArgs e)
		{
			System.Drawing.Point mousePosition = Control.MousePosition;
			System.Drawing.Point pt = base.PointToClient(mousePosition);
			if (this.btnCancel.Bounds.Contains(pt))
			{
				return;
			}
			try
			{
				double num = double.Parse(this.tbAngle.Text);
				if (num < 0.0 || num > 360.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				MessageBox.Show("旋转角度范围在0~360°之间。");
				this.tbAngle.Focus();
				this.tbAngle.SelectAll();
			}
		}

		private void TextToolForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void TextToolForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "TextTool.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cmbFont.Text);
			streamWriter.WriteLine(this.tbSize.Text);
			streamWriter.WriteLine(this.tbRowDistance.Text);
			streamWriter.WriteLine(this.tbPageWidth.Text);
			streamWriter.WriteLine(this.tbPageHeight.Text);
			streamWriter.WriteLine(this.tbPageDistance.Text);
			streamWriter.WriteLine(this.cmbAlign.Text);
			streamWriter.WriteLine(this.tbAngle.Text);
			streamWriter.WriteLine(this.cmbFormat.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string path = Path.GetTempPath() + "TextTool.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.cmbFont.Text = streamReader.ReadLine();
				this.tbSize.Text = streamReader.ReadLine();
				this.tbRowDistance.Text = streamReader.ReadLine();
				this.tbPageWidth.Text = streamReader.ReadLine();
				this.tbPageHeight.Text = streamReader.ReadLine();
				this.tbPageDistance.Text = streamReader.ReadLine();
				this.cmbAlign.Text = streamReader.ReadLine();
				this.tbAngle.Text = streamReader.ReadLine();
				this.cmbFormat.Text = streamReader.ReadLine();
				try
				{
					double.Parse(this.tbSize.Text);
				}
				catch
				{
					this.tbSize.Text = "4";
				}
				try
				{
					double.Parse(this.tbRowDistance.Text);
				}
				catch
				{
					this.tbRowDistance.Text = "0.4";
				}
				try
				{
					double.Parse(this.tbPageWidth.Text);
				}
				catch
				{
					this.tbPageWidth.Text = "200";
				}
				try
				{
					double.Parse(this.tbPageHeight.Text);
				}
				catch
				{
					this.tbPageHeight.Text = "200";
				}
				try
				{
					double.Parse(this.tbPageDistance.Text);
				}
				catch
				{
					this.tbPageDistance.Text = "7";
				}
				try
				{
					double.Parse(this.tbAngle.Text);
				}
				catch
				{
					this.tbAngle.Text = "0";
				}
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		private void TextToolForm_Resize(object sender, EventArgs e)
		{
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			base.Width = (int)(593.0 * num);
			if (base.Height < 500)
			{
				base.Height = 500;
			}
			this.richTextBox1.Height = base.Height - (int)(280.0 * num2);
			this.btnOk.Location = new System.Drawing.Point(this.btnOk.Location.X, base.Height - (int)(70.0 * num2));
			this.btnCancel.Location = new System.Drawing.Point(this.btnCancel.Location.X, base.Height - (int)(70.0 * num2));
		}

		private void TextToolForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
		}

		private void richTextBox1_Click(object sender, EventArgs e)
		{
			if (this.richTextBox1.SelectionLength != 0)
			{
				return;
			}
			Font selectionFont = this.richTextBox1.SelectionFont;
			if (this.richTextBox1.SelectionCharOffset == 0)
			{
				Font selectionFont2 = new Font(this.cmbFont.Text, float.Parse(this.tbSize.Text), selectionFont.Style, GraphicsUnit.Millimeter);
				this.richTextBox1.SelectionFont = selectionFont2;
				return;
			}
			if (this.richTextBox1.SelectionCharOffset < 0)
			{
				Font selectionFont3 = new Font(this.cmbFont.Text, float.Parse(this.tbSize.Text) / 2f, selectionFont.Style, GraphicsUnit.Millimeter);
				this.richTextBox1.SelectionFont = selectionFont3;
				return;
			}
			Font selectionFont4 = new Font(this.cmbFont.Text, float.Parse(this.tbSize.Text) / 2f, selectionFont.Style, GraphicsUnit.Millimeter);
			this.richTextBox1.SelectionFont = selectionFont4;
			this.richTextBox1.SelectionCharOffset = (int)float.Parse(this.tbSize.Text) / 2;
		}

		private string m_specialType = string.Empty;

		public List<Section> m_sections;

		public int m_length;

		public IDictionary<int, int> m_scripts = new Dictionary<int, int>();

		public IDictionary<int, int> m_aligns = new Dictionary<int, int>();

		public bool m_pickInScreen;

		private Color m_color = System.Drawing.Color.Red;
	}
}
